<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用车开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择用车开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用车结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择用车结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['v3:application:add']"
        >申请用车</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用车申请表id" align="center" prop="id" />
      <el-table-column label="申请人姓名" align="center" prop="username" />
      <el-table-column label="用车开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用车结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾照图片" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="出发地" align="center" prop="departureAddr" />
      <el-table-column label="目的地" align="center" prop="destinationAddr" />
      <el-table-column label="用车原因" align="center" prop="reason" />
      <el-table-column label="其它备注信息" align="center" prop="remark" />
      <el-table-column label="申请表状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.application_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="分配的车辆信息" align="center" prop="vehicleId" />
      <el-table-column label="驳回原因" align="center" prop="rejectReason" />
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === '10'" size="mini" type="danger" @click="revoke(scope.row)">撤回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用车申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="用车人" prop="username">
              <el-input v-model="form.username" placeholder="请输入用车人" />
            </el-form-item>
            <el-form-item label="车辆出发地" prop="departureAddr">
              <el-input v-model="form.departureAddr" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="用车事由" prop="reason">
              <el-input v-model="form.reason" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="驾照" prop="imgUrl">
              <image-upload v-model="form.imgUrl"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用时间" required>
              <el-date-picker
                v-model="form.startTime"
                type="date"
                placeholder="开始日期"
                value-format="yyyy-MM-dd"
                style="width: 100%; margin-bottom: 8px;"
              />
              <el-date-picker
                v-model="form.endTime"
                type="date"
                placeholder="结束日期"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
              />
            </el-form-item>
            <el-form-item label="车辆目的地" prop="destinationAddr">
              <el-input v-model="form.destinationAddr" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="审批人">
              <el-button size="mini" type="primary">tom</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplication, getApplication, delApplication, addApplication, updateApplication, revokeApplication } from "@/api/v3/application"

export default {
  name: "Application",
  dicts: ['application_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用车申请表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用车申请列表 */
    getList() {
      this.loading = true
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        username: null,
        startTime: null,
        endTime: null,
        imgUrl: null,
        departureAddr: null,
        destinationAddr: null,
        reason: null,
        remark: null,
        status: null,
        vehicleId: null,
        rejectReason: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加用车申请"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getApplication(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改用车申请"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addApplication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除用车申请编号为"' + ids + '"的数据项？').then(function() {
        return delApplication(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('v3/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    },
    revoke(row) {
      this.$modal.confirm('确定要撤回该申请吗？').then(() => {
        return revokeApplication(row.id)
      }).then(() => {
        this.$modal.msgSuccess('撤回成功')
        this.getList()
      })
    }
  }
}
</script>
